import {
  JupyterFrontEnd,
  JupyterFrontEndPlugin
} from '@jupyterlab/application';

// import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { ICommandPalette } from '@jupyterlab/apputils';
import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
import { ILauncher } from '@jupyterlab/launcher';
// import { ICollaborativeDrive } from '@jupyter/collaborative-drive';

// import { Widget } from '@lumino/widgets';

import { DocModelFactory, IDocModelFactoryOpts } from './modelFactory';

import { MindElixirWidgetFactory } from './widgetFactory';

/**
 * Initialization data for the myextension extension.
 */
const plugin: JupyterFrontEndPlugin<void> = {
  id: 'myextension:plugin',
  description: 'A JupyterLab extension- myextension.',
  autoStart: true,
  requires: [ICommandPalette, IFileBrowserFactory],
  optional: [ILauncher],
  activate: async (app: JupyterFrontEnd, 
                   commandPalette: ICommandPalette, 
                   fileBrowser: IFileBrowserFactory, 
                   
                   launcher: ILauncher | null) => {
    console.log('JupyterLab extension myextension is activated XXX!');
    console.log('ICommandPalette', commandPalette)

    const FACTORY_NAME: string = 'mind-elixir-file';
    const FILE_FORMAT: string = 'text';
    const CONTENT_TYPE: string = 'mind-elixir-file-doc';

    try {
      const { commands } = app;
      
      // 添加一个创建思维导图空文件的命令
      commands.addCommand('mindex:create-new', {
        label: '新建思维导图',
        iconClass: 'jp-MaterialIcon jp-ImageIcon',
        caption: '创建一个新的思维导图文件.',
        execute: args => {
          const cwd =
            args['cwd'] || fileBrowser.tracker.currentWidget?.model.path;

          commands
            .execute('docmanager:new-untitled', {
              path: cwd,
              type: 'file',
              ext: '.mindex'
            })
            .then(model =>
              commands.execute('docmanager:open', {
                path: model.path,
                factory: FACTORY_NAME
              })
            );
        }
      });

      // 添加创建思维导图命令到到启动器中
      if (launcher) {
        launcher.add({
          command: 'mindex:create-new',
          category: 'Other',
          rank: 1
        });
      }

      //
      // 添加模型工厂, 用于创建文件时, 生成对应的模型
      app.docRegistry.addModelFactory(new DocModelFactory({
        modelName: FACTORY_NAME,
        fileFormat: FILE_FORMAT,
        contentType: CONTENT_TYPE,
      } as IDocModelFactoryOpts))

      //
      // 添加widget工厂, 用于创建文件时, 生成对应的视图
      app.docRegistry.addWidgetFactory(new MindElixirWidgetFactory({
        name: FACTORY_NAME,
        modelName: FACTORY_NAME,
        fileTypes: ['mindex'],
        defaultFor: ['mindex'],
      }));

      //
      // 添加文件类型, 用于有创建新文件时, 显示在列表中
      console.log('docRegistry mind-elixir-file')
      app.docRegistry.addFileType({
        name: FACTORY_NAME,
        displayName: '基于Mind Elixir的思维导图',
        fileFormat: FILE_FORMAT as any,
        contentType: "file",
        extensions: ['.mindex'],
        iconClass: 'jp-FileIcon',
        mimeTypes: ['application/text'],
      }, [FACTORY_NAME]);

    } catch (error: any) {
      console.error("error = " + error.message, "stack = ", error.stack);
    }
  }
};

export default plugin;
